<template>
  <div class="content">
    <div class="category">
      <div class="category__item" :class="item._id === currentId ? 'category__item--active' : ''"
        v-for="item in categoryList" :key="item._id" @click="handleClick(item)">{{ item.name }}</div>
    </div>
    <div class="product">
      <div class="product__item" v-for="product in products" :key="product._id">
        <img class="product__item__img" :src="product.imgUrl" alt="">
        <div class="product__item__detail">
          <h4 class="product__item__title">{{ product.name }}</h4>
          <p class="product__item__sales">月售{{ product.sales }}件</p>
          <p class="product__item__price">
            <span class="yen">&yen;{{ product.price }}</span>
            <span class="origin">&yen; {{ product.oldPrice }}</span>
          </p>
        </div>
        <div class="product__number">
          <span class="product__number__minus" @click="handleMinus(shopId, product._id, product)">-</span>
          {{ cartList?.[shopId]?.productList?.[product._id]?.count || 0 }}
          <span class="product__number__plus" @click="handlePlus(shopId, product._id, product)">+</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue"
import { categories, getProducts } from "../../../../mock/product";
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
const route = useRoute()
const categoryList = ref([])
const currentId = ref(0)
const products = ref([])
const shopId = ref(route.params.id)
onMounted(async () => {
  categoryList.value = categories(route.params.id)
  currentId.value = categoryList.value[0]._id
  products.value = await getProducts(categoryList.value[0].category)
});
const handleClick = async (item) => {
  products.value = await getProducts(item.category);
};
const store = useStore()
const cartList = store.state.cartList
const handlePlus = (shopId, productId, productInfo) => {
  store.commit('changeCartList', { shopId, productId, productInfo, type: 'plus' })

}
const handleMinus = (shopId, productId, productInfo) => {
  store.commit('changeCartList', { shopId, productId, productInfo, type: 'minus' })
}
  ;
</script>

<style lang="scss" scoped>
@import '@/css/variables.scss';
@import '@/css/mixins.scss';

.content {
  display: flex;
  position: absolute;
  top: 1.5rem;
  bottom: 0.5rem;
  left: 0;
  right: 0;
}

.category {
  width: 0.76rem;
  background-color: $search-bgColor;
  overflow-y: scroll;
  height: 100%;

  &__item {
    line-height: 0.4rem;
    text-align: center;
    font-size: .14rem;
  }

  &__item--active {
    background-color: $bgColor;
  }

}

.product {
  flex: 1;
  overflow-y: scroll;

  &__item {
    position: relative;
    display: flex;
    padding: 0.12rem 0;
    margin: 0 0.16rem;
    border-bottom: 0.01rem solid $content-bgColor;

    &__img {
      width: 0.68rem;
      height: 0.68rem;
      margin-right: 0.16rem;
    }

    &__detail {
      overflow: hidden;
    }

    &__title {
      margin: 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $content-fontColor;
      @include ellipses;
    }

    &__sales {
      margin: 0.06rem 0;
      line-height: 0.16rem;
      font-size: 0.12rem;
      color: $content-fontColor;
    }

    &__price {
      margin: 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $content-fontColor;

      .yen {
        color: $highlight-fontColor;
        font-size: 0.14rem;
      }

      .origin {
        line-height: 0.2rem;
        font-size: 0.12rem;
        color: $light-fontColor;
        text-decoration: line-through;
        margin-left: 0.06rem;
      }
    }
  }

  &__number {
    position: absolute;
    right: 0;
    bottom: 0.12rem;

    &__minus,
    &__plus {
      display: inline-block;
      width: 0.2rem;
      height: 0.2rem;
      line-height: 0.16rem;
      border-radius: 50%;
      font-size: 0.2rem;
      text-align: center;
    }

    &__minus {
      border: 0.01rem solid $medium-fontColor;
      color: $medium-fontColor;
      margin-right: 0.05rem;
    }

    &__plus {
      margin-left: 0.05rem;
      background-color: $btn-bgColor;
      color: $bgColor;
    }
  }
}
</style>
